<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant\controller;

use app\tenant\model\TenantBusinessModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminBusinessController extends AdminBaseController
{
    /**
     * 商户主营业务管理
     * @adminMenu(
     *     'name'   => '商户主营业务管理',
     *     'parent' => 'tenant/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户主营业务管理',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $keywords = $this->request->param('keywords');

        $shop_info = new TenantBusinessModel();

        $list = $shop_info->getList($keywords);

        // 获取分页显示
        $page = $list->render();

        $this->assign('page',$page);
        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * 商户主营业务 添加
     * @adminMenu(
     *     'name'   => '添加商户主营业务',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加商户主营业务',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 商户主营业务 添加提交
     * @adminMenu(
     *     'name'   => '添加商户主营业务提交',
     *     'parent' => 'add',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加商户主营业务提交',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function addPost()
    {
        $param = $this->request->param('post');
        $photo_urls = $this->request->param('photo_urls');
        $data = [
            'name'=>$param['name'],
            'list_order'=>$param['list_order'],
        ];
        if($photo_urls){
            $data['thumbnail']=cmf_get_image_preview_url($photo_urls[0]);
        }
        $res = Db::name('tenant_business_scope')->insertGetId($data);
        if($res){
            $this->success('新建主营业务成功', 'index');
        }else{
            $this->error('新建主营业务失败');

        }
    }

    /**
     * 编辑主营业务
     * @adminMenu(
     *     'name'   => '编辑商户主营业务',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户主营业务',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $service = Db::name('tenant_business_scope')->where('id',$id)->find();

        $this->assign('service', $service);
        return $this->fetch();

    }

    /**
     * 编辑商户主营业务提交
     * @adminMenu(
     *     'name'   => '编辑商户主营业务提交',
     *     'parent' => 'edit',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户主营业务提交',
     *     'param'  => ''
     * )
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPost()
    {
        $param   = $this->request->param('post');
        $photo_urls = $this->request->param('photo_urls');
        $data = [
            'name'=>$param['name'],
            'list_order'=>$param['list_order'],
        ];
        if($photo_urls){
            $data['thumbnail']=cmf_get_image_preview_url($photo_urls[0]);
        }
        $res = Db::name('tenant_business_scope')->where('id',$param['id'])->update($data);
        if($res){
            $this->success('修改主营业务成功', 'index');
        }else{
            $this->error('修改主营业务失败');

        }
    }

    /**
     * 编辑商户主营业务禁用
     * @adminMenu(
     *     'name'   => '编辑商户主营业务禁用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户主营业务禁用',
     *     'param'  => ''
     * )
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ban()
    {
        $id   = $this->request->param();

        $res = Db::name('tenant_business_scope')->where(['id'=>$id])->update(['status'=>0]);

        if($res){
            $this->success('禁用主营业务成功');
        }else{
            $this->error('禁用主营业务失败');

        }
    }
        /**
     * 编辑商户主营业务启用
     * @adminMenu(
     *     'name'   => '编辑商户主营业务启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑商户主营业务启用',
     *     'param'  => ''
     * )
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancelBan()
    {
        $id   = $this->request->param();

        $res = Db::name('tenant_business_scope')->where(['id'=>$id])->update(['status'=>1]);

        if($res){
            $this->success('启用主营业务成功');
        }else{
            $this->error('启用主营业务失败');

        }
    }
}